//
//  File.swift
//  DemoTest
//
//  Created by admin on 2022/4/24.
//

import SwiftUI

import UIKit

struct ShoppingVC: View {
    public var count = 0
    @State var strawberryCanDrag = true
    @State var peachCanDrag = true
    @State var flourCanDrag = true
    @State var creamOneCanDrag = true
    @State var creamTwoCanDrag = true
    @State var watermelonCanDrag = true
    @State var grapeCanDrag = true
    @State var pineappleCanDrag = true
    @State var cannext = false
    @State var need:Int = 0
    @State var showContentView: Bool = false
    @State var nextDial: Bool = false
    @State var strawberryLocation = CGPoint(x: UIScreen.main.bounds.width/10 + 100, y: UIScreen.main.bounds.height/3 + CGFloat(20))
    
    @State var peachLocation = CGPoint(x: UIScreen.main.bounds.width/10, y: UIScreen.main.bounds.height/2 + CGFloat(40))
    
    
    @State var flourLocation =  CGPoint(x: UIScreen.main.bounds.width/3 + CGFloat(40), y: UIScreen.main.bounds.height/CGFloat(1.5) + CGFloat(20))
    
    @State var creamOneLocation =  CGPoint(x: UIScreen.main.bounds.width/2 + CGFloat(80), y: UIScreen.main.bounds.height/3 + CGFloat(15))
    
    @State var creamTwoLocation =  CGPoint(x: UIScreen.main.bounds.width - CGFloat(200), y: UIScreen.main.bounds.height/3 + CGFloat(20))
    
    @State var watermelonLocation =  CGPoint(x: UIScreen.main.bounds.width/3 + CGFloat(150), y: UIScreen.main.bounds.height/CGFloat(1.5) + CGFloat(35))
    
    @State var grapelonLocation =  CGPoint(x: UIScreen.main.bounds.width - CGFloat(300), y: UIScreen.main.bounds.height/3 + CGFloat(50))
    
    @State var pineappleLocation =  CGPoint(x: UIScreen.main.bounds.width/9 + CGFloat(80) , y: UIScreen.main.bounds.height/2 + CGFloat(20))
    
    
    @GestureState var strawberryLocationPosition = CGPoint(x: UIScreen.main.bounds.width/10 + 100, y: UIScreen.main.bounds.height/3 + 42)
    
    @GestureState var peachLocationPosition = CGPoint(x: UIScreen.main.bounds.width/10, y: UIScreen.main.bounds.height/2 + CGFloat(40))
    
    @GestureState var flour = CGPoint(x: UIScreen.main.bounds.width/3 + CGFloat(40), y: UIScreen.main.bounds.height/CGFloat(1.5) + CGFloat(20))
    
    @GestureState var creamOne = CGPoint(x: UIScreen.main.bounds.width/2 + CGFloat(80), y: UIScreen.main.bounds.height/3 + CGFloat(15))
    
    @GestureState var creamTwo = CGPoint(x: UIScreen.main.bounds.width - CGFloat(200), y: UIScreen.main.bounds.height/3 + CGFloat(20))
    
    @GestureState var watermelon = CGPoint(x: UIScreen.main.bounds.width/3 + CGFloat(150), y: UIScreen.main.bounds.height/CGFloat(1.5) + CGFloat(35))
    
    @GestureState var grape = CGPoint(x: UIScreen.main.bounds.width - CGFloat(300), y: UIScreen.main.bounds.height/3 + CGFloat(50))
    
    @GestureState var pineapple = CGPoint(x: UIScreen.main.bounds.width/9 + CGFloat(80) , y: UIScreen.main.bounds.height/2 + CGFloat(20))
    
    var body: some View {
        ZStack{
            Group{
                Image("Shelf")
                    .resizable()
                    .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
                
                
                
                //
                //            770,412
                
                
                Image("strawberry")
                    .resizable()
                    .frame(width: 100, height: 100)
                
                    .position(strawberryLocation)
                
                    .gesture(
                        DragGesture()
                            .onChanged { state in
                                if strawberryCanDrag {
                                    strawberryLocation = state.location
                                }
                                
                            }
                            .onEnded{ item in
                                
                                if strawberryLocationPosition.x > 712 && strawberryLocationPosition.y >
                                    400 {
                                    need += 1
                                    withAnimation{
                                        strawberryLocation.x = 720
                                    }
                                    strawberryCanDrag.toggle()
                                }else {
                                    withAnimation {
                                        strawberryLocation = CGPoint(x: UIScreen.main.bounds.width/10 + 100, y: UIScreen.main.bounds.height/3 + CGFloat(20))
                                    }
                                }
                            }
                            .updating($strawberryLocationPosition){currtenState, pastlocation, transiton in
                                pastlocation = currtenState.location
                                print("locationPosition.x: \(strawberryLocationPosition.x),locationPosition.y:\(strawberryLocationPosition.y)")
                                transiton.animation = .easeInOut
                                
                                
                            }
                        
                    )
                
                Image("Peach")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 100, height: 100)
                    .position(peachLocation)
                    .gesture(
                        DragGesture()
                            .onChanged { state in
                                if peachCanDrag {
                                    peachLocation = state.location
                                }
                                
                            }
                            .onEnded{ item in
                                
                                if peachLocationPosition.x > 712 && peachLocationPosition.y > 400 {
                                    need+=1
                                    withAnimation {
                                        peachLocation.x = 720
                                    }
                                    peachCanDrag.toggle()
                                }else {
                                    withAnimation {
                                        peachLocation = CGPoint(x: UIScreen.main.bounds.width/10, y: UIScreen.main.bounds.height/2 + CGFloat(40))
                                    }
                                }
                            }
                            .updating($peachLocationPosition, body: {currtenState, pastlocation, transiton in
                                pastlocation = currtenState.location
                                transiton.animation = .easeInOut
                            }
                                     )
                    )
                Image("Flour")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 180, height: 180)
                    .position(flourLocation)
                    .gesture(
                        DragGesture()
                            .onChanged { state in
                                if flourCanDrag {
                                    flourLocation = state.location
                                }
                                
                            }
                            .onEnded{ item in
                                print("flourLocation:\(flourLocation)")
                                if flourLocation.x > 712 && flourLocation.y > 400 {
                                    need+=1
                                    withAnimation{
                                        flourLocation.x = 712
                                    }
                                    flourCanDrag.toggle()
                                }else {
                                    withAnimation {
                                        flourLocation =  CGPoint(x: UIScreen.main.bounds.width/3 + CGFloat(40), y: UIScreen.main.bounds.height/CGFloat(1.5) + CGFloat(20))
                                    }
                                }
                            }
                            .updating($flour, body: {currtenState, pastlocation, transiton in
                                pastlocation = currtenState.location
                                transiton.animation = .easeInOut
                            }
                                     )
                    )
                
                Image("CreamOne")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 200, height: 200)
                    .position(creamOneLocation)
                    .gesture(
                        DragGesture()
                            .onChanged { state in
                                if creamOneCanDrag {
                                    creamOneLocation = state.location
                                }
                                
                            }
                            .onEnded{ item in
                                
                                if creamOne.x > 712 && creamOne.y > 400 {
                                    need+=1
                                    withAnimation{
                                        creamOneLocation.x = 720
                                    }
                                    creamOneCanDrag.toggle()
                                }else {
                                    withAnimation {
                                        creamOneLocation = CGPoint(x: UIScreen.main.bounds.width/2 + CGFloat(80), y: UIScreen.main.bounds.height/3 + CGFloat(15))
                                    }
                                }
                            }
                            .updating($creamOne, body: {currtenState, pastlocation, transiton in
                                pastlocation = currtenState.location
                                transiton.animation = .easeInOut
                            }
                                     )
                    )
                Image("CreamTwo")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 120, height: 120)
                    .position(creamTwoLocation)
                    .gesture(
                        DragGesture()
                            .onChanged { state in
                                if creamTwoCanDrag {
                                    creamTwoLocation = state.location
                                }
                                
                            }
                            .onEnded{ item in
                                
                                if creamTwo.x > 712 && creamTwo.y > 400 {
                                    need+=1
                                    withAnimation{
                                        creamTwoLocation.x = 720
                                    }
                                    creamTwoCanDrag.toggle()
                                }else {
                                    withAnimation {
                                        creamTwoLocation = CGPoint(x: UIScreen.main.bounds.width - CGFloat(200), y: UIScreen.main.bounds.height/3 + CGFloat(20))
                                    }
                                }
                            }
                            .updating($creamTwo, body: {currtenState, pastlocation, transiton in
                                pastlocation = currtenState.location
                                transiton.animation = .easeInOut
                            }
                                     )
                    )
                Image("Grape")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 200, height: 200)
                    .position(grapelonLocation)
                    .gesture(
                        DragGesture()
                            .onChanged { state in
                                if grapeCanDrag {
                                    grapelonLocation = state.location
                                }
                                
                            }
                            .onEnded{ item in
                                
                                if grape.x > 712 && grape.y > 400 {
                                    withAnimation{
                                        need+=1
                                        grapelonLocation.x = 720
                                    }
                                    grapeCanDrag.toggle()
                                }else {
                                    withAnimation {
                                        grapelonLocation = CGPoint(x: UIScreen.main.bounds.width - CGFloat(300), y: UIScreen.main.bounds.height/3 + CGFloat(50))
                                    }
                                }
                            }
                            .updating($grape, body: {currtenState, pastlocation, transiton in
                                pastlocation = currtenState.location
                                transiton.animation = .easeInOut
                            }
                                     )
                    )
                Image("Watermelon")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 200, height: 150)
                    .position(watermelonLocation)
                    .gesture(
                        DragGesture()
                            .onChanged { state in
                                if watermelonCanDrag {
                                    watermelonLocation = state.location
                                }
                                
                            }
                            .onEnded{ item in
                                
                                if watermelon.x > 712 && watermelon.y > 400 {
                                    need+=1
                                    withAnimation{
                                        watermelonLocation.x = 720
                                    }
                                    watermelonCanDrag.toggle()
                                }else {
                                    withAnimation {
                                        watermelonLocation = CGPoint(x: UIScreen.main.bounds.width/3 + CGFloat(150), y: UIScreen.main.bounds.height/CGFloat(1.5) + CGFloat(35))
                                    }
                                }
                            }
                            .updating($watermelon, body: {currtenState, pastlocation, transiton in
                                pastlocation = currtenState.location
                                transiton.animation = .easeInOut
                            }
                                     )
                    )
                
                Image("pineapple")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 200, height: 150)
                    .position(pineappleLocation)
                    .gesture(
                        DragGesture()
                            .onChanged { state in
                                if pineappleCanDrag {
                                    pineappleLocation = state.location
                                }
                                
                            }
                            .onEnded{ item in
                                
                                if pineapple.x > 712 && pineapple.y > 400 {
                                    need+=1
                                    withAnimation{
                                        pineappleLocation.x = 720
                                    }
                                    pineappleCanDrag.toggle()
                                }else {
                                    withAnimation {
                                        pineappleLocation =  CGPoint(x: UIScreen.main.bounds.width/9 + CGFloat(80) , y: UIScreen.main.bounds.height/2 + CGFloat(20))
                                    }
                                }
                            }
                            .updating($pineapple, body: {currtenState, pastlocation, transiton in
                                pastlocation = currtenState.location
                                transiton.animation = .easeInOut
                            }
                                     )
                    )
                ZStack {
                    if !nextDial {
                      VStack{
                          Spacer()
                          Image("text1")
                              .resizable()
                              .aspectRatio(contentMode: .fit)
                      }
                      .background(Color.white.opacity(0.01))
                        .frame(width: screen.width, height: screen.height)
                        .onTapGesture {
                            nextDial = true
                        }
                  }
                    
                    if need == 8 {
                      VStack{
                          Spacer()
                          Image("text2")
                              .resizable()
                              .aspectRatio(contentMode: .fit)
                      }
                      .background(Color.white.opacity(0.01))
                        .frame(width: screen.width, height: screen.height)
                        .onTapGesture {
                            nextDial = true
                        }
                  }
                }
                
                 
            
              
            }
            
            
            
            Image("Thinking")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .offset(x: UIScreen.main.bounds.width/5, y: 100)
                .frame(width:UIScreen.main.bounds.width / 3, height: UIScreen.main.bounds.height / 3)
            //        .overlay(
            //
                            
            //        )
            //        .ignoresSafeArea()

            
            if need == 8 {
                VStack {
                    Spacer()
                    HStack {
                        Spacer()
                        Button  {
                            showContentView = true
                        } label: {
                            Image("nextButton")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: 50, height: 50)
                                .padding()
                        }
                        .fullScreenCover(isPresented: self.$showContentView, content: {ContentView()})
                    }
                }
            }
            
            
        }
        
        
    }
    
    
}









struct ShoppingVC_Previews: PreviewProvider {
    static var previews: some View {
        ShoppingVC()
            .previewInterfaceOrientation(.landscapeLeft)
    }
}


